var fs      = require("fs");
var async   = require("async");
var mysql   = require("mysql");
var moment  = require("moment");
var Q       = require("q");
var util    = require("util")



var pool = mysql.createPool({
	host     : "localhost",
	user     : "root",
	password : "root",
	database : "logcount",
	port     : 3306,
	connectionLimit: 10000,
	waitForConnections: true
});

function execTrans(sqlParams, callback) {
    var resp_result = {};
    pool.getConnection(function (err, connection) {
        if (err) {
					console.log("数据库连接："+err);
          return callback(null);
        }
        connection.beginTransaction(function (err) {
            if (err) {
							console.log(err);
							return callback(null);
            }
            console.log("开始执行transaction，共执行" + sqlParams.length + "条数据");
            var funcAry = [];
            sqlParams.forEach(function (sql_param) {
								console.log(sql_param);
                var temp = function (cb) {
                    connection.query(sql_param,function (tErr, rows, fields) {
                      if (tErr) {
                        connection.rollback(function () {
                          console.log("事务失败，" + sql_param + "，ERROR：" + tErr);
                          throw tErr;
                        });
                      }else{
                        return cb(null, 'ok');
                      }
                    })
                };
                funcAry.push(temp);
            });
            async.series(funcAry, function (err, result) {
                if (err) {
                    connection.rollback(function (err) {
                        console.log("transaction error: " + err);
                        connection.release();
												return callback(null);
                        // return callback(err, null);
                    });
                } else {
                    connection.commit(function (err, info) {
                        console.log("transaction info: " + JSON.stringify(info));
                        if (err) {
                            console.log("执行事务失败，" + err);
                            connection.rollback(function (err) {
                                console.log("rollback error: " + err);
                                connection.release();
                                return callback(null);
                            });
                        } else {
                            connection.release();
                            return callback(info);
                        }
                    })
                }
            })
        });
    });
}

//做非事物处理sql
function sql_allquery(sql, callback) {
  pool.getConnection(function (err, connection) {
      if (err) {
        return callback(err, null);
      }
      connection.query(sql, function(err, result){
          console.log("SQL: ", sql);
          if (err) {
              console.log("[SELECT ERROR] - ", err.message);
              if (callback) {
                  callback(null);
              }
              // connection.end();
							connection.release();
              return;
          }
          if (callback) {
              callback(result);
          }
          //connection.end();
					connection.release();
      });
  });
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
exports.execTrans = execTrans;
exports.sql_allquery = sql_allquery;
